package com.example.dessertstoressm.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dessertstoressm.Mapper.AccountMapper;
import com.example.dessertstoressm.Mapper.BannerDataMapper;
import com.example.dessertstoressm.Mapper.ProfileMapper;
import com.example.dessertstoressm.Mapper.SignOnMapper;
import com.example.dessertstoressm.Service.AccountService;
import com.example.dessertstoressm.domain.Account;
import com.example.dessertstoressm.domain.BannerData;
import com.example.dessertstoressm.domain.Profile;
import com.example.dessertstoressm.domain.SignOn;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private SignOnMapper signOnMapper;
    @Resource
    private ProfileMapper profileMapper;
    @Resource
    private BannerDataMapper bannerDataMapper;

    public Account getOther(Account account){
        QueryWrapper<Profile> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("userid",account.getUsername());
        Profile profile = this.profileMapper.selectOne(wrapper2);
        account.setProfile(profile);

        QueryWrapper<BannerData> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("favcategory",profile.getFavouriteCategoryId());
        BannerData bannerData = this.bannerDataMapper.selectOne(wrapper3);
        account.setBannerData(bannerData);

        return account;
    }

    @Override
    public Account getAccountByUsername(String username){
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",username);
        Account account = this.accountMapper.selectOne(wrapper);
        if(account != null){
            QueryWrapper<SignOn> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("username",account.getUsername());
            SignOn signOn = this.signOnMapper.selectOne(wrapper1);
            account.setSignOn(signOn);
            account = getOther(account);
            return account;
        }else {
            return null;
        }
    }

    @Override
    public Account getAccountByUsernameAndPassword(String username, String password){
        QueryWrapper<SignOn> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username).eq("password",password);
        SignOn signOn = this.signOnMapper.selectOne(wrapper);
        if(signOn != null){
            QueryWrapper<Account> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("userid",signOn.getUsername());
            Account account = this.accountMapper.selectOne(wrapper1);
            account.setSignOn(signOn);
            account = getOther(account);
            return account;
        }else {
            return null;
        }
    }

    @Override
    public void insertAccount(Account account){
        this.accountMapper.insert(account);
        this.signOnMapper.insert(account.getSignOn());
        this.profileMapper.insert(account.getProfile());
    }

    @Override
    public void updateAccount(Account account){
        QueryWrapper<Account> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("userid",account.getUsername());
        QueryWrapper<SignOn> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("username",account.getUsername());
        QueryWrapper<Profile> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("userid",account.getUsername());
        this.accountMapper.update(account,wrapper1);
        this.signOnMapper.update(account.getSignOn(),wrapper2);
        this.profileMapper.update(account.getProfile(),wrapper3);
    }

    @Override
    public Account getAccountByPhone(String phone){
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        Account account = this.accountMapper.selectOne(wrapper);
        if(account != null){
            QueryWrapper<SignOn> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("username",account.getUsername());
            SignOn signOn = this.signOnMapper.selectOne(wrapper1);
            account.setSignOn(signOn);
            account = getOther(account);
            return account;
        }else {
            return null;
        }
    }
}
